package com.example.queryweatherdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private static final String TAG="tag";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }
    private void initView(){
        Button btnQuery=findViewById(R.id.btn_query);
        btnQuery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendPost();
            }
        });
    }
    private void sendPost(){
        new Thread(){
            @Override
            public void run() {
                super.run();
                try {
                    URL url=new URL("http://apis.juhe.cn/simpleWeather/query?");
                    HttpURLConnection connection= (HttpURLConnection) url.openConnection();
                    connection.setConnectTimeout(10*1000);
                    connection.setReadTimeout(10*1000);
                    connection.setRequestMethod("POST");
                    connection.setUseCaches(false);
                    connection.setDoOutput(true);
                    connection.setDoInput(true);

                    String param="city="+URLEncoder.encode("西安","UTF-8")+"&key="+ URLEncoder.encode("e9cc240a2e1eac66e7ca4accd848d3bb","UTF-8");
                    OutputStream os=connection.getOutputStream();
                    os.write(param.getBytes());
                    //输出流写入完成后刷新
                    os.flush();
                    if (connection.getResponseCode()==HttpURLConnection.HTTP_OK){
                        InputStream inputStream=connection.getInputStream();
                        String result=inputStreamReader(inputStream);

                       RealtimeModel list=GsonUtil.realtimeWeather(result);

                            Log.d(TAG,"realtime: temperature="+list.getTemperature()+" humidity="+list.getHumidity()+
                                    " info="+list.getInfo()+" wid="+list.getWid()+" direct="+list.getDirect()+
                                    " power="+list.getPower()+" aqi="+list.getAqi());

                        FutureModel[] flist=GsonUtil.futureWeather(result);
                        for (FutureModel future:flist) {
                            Log.d(TAG,"未来五天天气:future date="+future.getDate()+" temperature="+future.getTemperature()+
                                    " weather="+future.getWeather()+" wid:day="+future.getWid().getDay()
                                    +"night="+future.getWid().getNight());

                        }

                    }

                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }.start();

    }
    private String inputStreamReader(InputStream inputStream) throws IOException {
        InputStreamReader inputStreamReader=new InputStreamReader(inputStream);
        BufferedReader bufferedReader=new BufferedReader(inputStreamReader);
        StringBuffer buffer=new StringBuffer();
        String str="";
        while(null!=(str=bufferedReader.readLine())){
            buffer.append(str);
        }
        bufferedReader.close();
        inputStreamReader.close();
        return buffer.toString();

    }
}